package com.wjy.sfhcore.itf.impl;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;

import com.wjy.sfhcore.ui.activity.CoreActivity;

/**
 * Fragment页面跳转
 *
 * @author wjy
 */
public class FragmentIntentFactory extends IntentFactory {
    private Fragment fragment;

    public FragmentIntentFactory(Fragment fragment) {
        super(fragment.getActivity());
        this.fragment = fragment;
    }

    private FragmentTransaction initFragmentTransaction() {
        return fragment.getFragmentManager().beginTransaction();
    }

    public void addFragment(int containerId, Fragment fragment) {
        FragmentTransaction transaction = initFragmentTransaction();
        transaction.add(containerId, fragment);
        transaction.commitAllowingStateLoss();
    }

    public void replaceFragment(int containerId, Fragment newFragment) {
        replaceFragment(containerId, newFragment, true);
    }


    public void replaceFragment(int contentID, Fragment newFragment,
                                boolean addToBackStack) {
        FragmentTransaction transaction = initFragmentTransaction();
        // transaction.setCustomAnimations(R.anim.push_left_in,
        // R.anim.push_left_out, R.anim.push_right_in,
        // R.anim.push_right_out);
        transaction.replace(contentID, newFragment);
        if (addToBackStack) {
            transaction.addToBackStack(null);
        }
        transaction.commitAllowingStateLoss();
    }


    public void goToOthersForResult(Class<?> cls, Bundle bundle, int requestCode) {
        Intent intent = new Intent(fragment.getActivity(), cls);
        intent.putExtra(CoreActivity.PARAM_INTENT, bundle);
        fragment.startActivityForResult(intent, requestCode);
    }

    public void backForResult(Class<?> cls, Bundle bundle, int resultCode) {
        Intent intent = new Intent();
        if (cls != null) {
            intent.setClass(fragment.getActivity(), cls);
        }
        intent.putExtra(CoreActivity.PARAM_INTENT, bundle);
        fragment.getActivity().setResult(resultCode, intent);
        fragment.getActivity().finish();

    }

}
